package cn.jiyun.fenlei.product.controller;

import cn.jiyun.common.vo.CommonResult;
import cn.jiyun.fenlei.product.config.AliyunOSSClientUtil;
import cn.jiyun.fenlei.product.service.ProductService;
import cn.jiyun.model.Product;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private AliyunOSSClientUtil aliyunOSSClientUtil;
    @PostMapping("/findAll")
    public CommonResult findAll(@RequestBody Product product){
     List<Product> productList= productService.findAll(product);
        return CommonResult.success(productList);
    }
    @PutMapping("/updatestatus")
    public CommonResult updatestatus(Integer id){
        productService.updatestatus(id);
        return CommonResult.success();
    }
    @GetMapping("/yijifindAll")
    public CommonResult yijifindAll(){
     List<Product> productList=    productService.yijifindAll();
        return CommonResult.success(productList);
    }
    @PostMapping("/findAdd")
    public  CommonResult findAdd(@RequestBody Product product){
        product.setDel(0);
        productService.save(product);
        return CommonResult.success();
    }
    @PostMapping("/updatebyid")
    public  CommonResult updatebyid(@RequestBody Product product){
        productService.updateById(product);
        return CommonResult.success();
    }
//    @RequestMapping("/upload")
//    public String uploader(MultipartFile file) throws IOException {
//        if (file.getSize()>0&&file!=null){
//            String filename = file.getOriginalFilename();
//            String substring = filename.substring(filename.lastIndexOf("."));
//            String s = UUID.randomUUID().toString();
//            file.transferTo(new File("D:/upload/"+s+substring));
//            return "http://localhost:9092/images/"+s+substring;
//        }
//        return null;
//    }
//@PostMapping("/upload")
//public  String uploader(@RequestBody MultipartFile file){
//
//    /**返回上传到oss的路径*/
//    String url = productService.uploader(file);
//
//    return url;
//}
//    @DeleteMapping("/deletebyid")
//    public CommonResult deletebyid(Integer id){
//        Product byId = productService.getById(id);
//        if (byId.getStatus()==1){
//            return CommonResult.error("状态是开启状态");
//        }
//        if (byId.getPid()==0){
//            List<Product> list = productService.list();
//            if (list.size()>0){
//                return CommonResult.error("该分类下面有子分类");
//            }
//        }
//            byId.setDel(1);
//            productService.updateById(byId);
//            return CommonResult.success();
//    }
//}
@RequestMapping("/upload")
public  String uploader( MultipartFile file) throws Exception {

    String s = aliyunOSSClientUtil.uploadImg2Oss(file);
    String imgUrl = aliyunOSSClientUtil.getImgUrl(s);
//    String url = aliyunOSSClientUtil.getUrl(s);
    return imgUrl;

}
@GetMapping("/fenleiname/{id}")
    public  String fenleiname(@PathVariable Integer id){
    Product byId = productService.getById(id);
    return byId.getName();
}
}